package com.线程Thread.Exercise;

public class Exercise03 {
    public static void main(String[] args) {
        T t = new T();
        new Thread(t,"A").start();
        new Thread(t,"B").start();
    }
}

class T implements Runnable {
    int balance = 10000;

    @Override
    public void run() {
        while (true) {
            synchronized (this) {
                if (balance >= 1000) {
                    balance -= 1000;
                    System.out.println("用户 "+Thread.currentThread().getName()+"\t取款成功\t余额为" + balance);

                } else {
                    System.out.println("余额不足，已退卡");
                    break;
                }
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                System.out.println("休眠失败");
            }
        }


    }
}